package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.PatternSyntaxException;

import javax.swing.DefaultRowSorter;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import br.edu.sistemaacademico.strategy.J6SortLister;

public class TabelaModelo {

	// public List<Object> campos = new ArrayList<Object>();
	private List<Object> valores = new ArrayList<Object>();
	// private List<Object> tamanho = new ArrayList<Object>();
	DefaultTableModel model;
	J6SortLister sort;
	public TabelaModelo(List<Object> campos, List<Object> tamanho, JTable tabela) {
		super();
		model = (DefaultTableModel) tabela.getModel();
		model.setNumRows(0);
		model.setColumnCount(0);
		tabela.getModel();

		sort = new J6SortLister();
		sort.sort(tabela, model);

		for (int i = 0; i < campos.size(); i++)
			model.addColumn(campos.get(i).toString());
		for (int i = 0; i < tamanho.size(); i++)
			tabela.getColumnModel().getColumn(i).setPreferredWidth(
					Integer.parseInt(tamanho.get(i).toString()));

		// --

	}

	public void setValores(List<Object> valores) {
		this.valores.clear();
		this.valores = valores;
		// for(int i=1;i<valores.size();i++)
		model.addRow(valores.toArray());
	}

	public JTable getTable() {
		return getTable();
	}

	public void clear() {
		// model.removeRow(0);
	}

	public void busca(final JTextField filterText) {
		filterText.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				setBuscar(filterText);
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				setBuscar(filterText);
			}

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				setBuscar(filterText);
			}
		});
	}

	public void setBuscar(final JTextField filterText) {
		sort.filter(filterText);
	}
	
	public void delete(int row){
		sort.delete(row);
	}

}
